﻿@using eLoan.Models;
@model BorrowerAddEditModel

@{
    ViewBag.Title = "Add/Edit Borrower";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Add/Edit Borrower</h2>
        <ol class="breadcrumb">
            <li>
                Masterfile
            </li>
            <li>
                <a href="@Url.Action("Borrowers", "Masterfile")">Address Book</a>
            </li>
            <li class="active">
                <strong>Add/Edit Borrower</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
            @using (Html.BeginForm("Save", "Borrower", FormMethod.Post, new { id = "saveForm" }))
            {
                <div class="ibox-title">
                    <div id="valBorrowerNameExists" style="display:none">Name already exists</div>
                    <div>@Html.ValidationMessageFor(m => m.BorrowerModel.Borrower.BorrowerName)</div>
                    <div>@Html.ValidationMessageFor(m => m.BorrowerModel.Borrower.BranchCode)</div>
                    <div>@Html.ValidationMessageFor(m => m.BorrowerModel.Borrower.AgentID)</div>
                    <div>@Html.ValidationMessageFor(m => m.BorrowerModel.Borrower.Address)</div>
                    <div>@Html.ValidationMessageFor(m => m.BorrowerModel.Borrower.ContactNumbers)</div>
                    <div>@Html.ValidationMessageFor(m => m.BorrowerModel.Borrower.EmailAddresses)</div>
                </div>
                <div class="ibox-content">

                    
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <fieldset>
                            <div class="col-lg-8">
                                <table class="table table-bordered table-striped">
                                    <tr>
                                        <th>
                                            @if (Model.DBCommand == "Edit")
                                            { @Html.HiddenFor(m => m.BorrowerModel.Borrower.ID) }
                                            @Html.LabelFor(m => m.BorrowerModel.Borrower.BorrowerName)
                                        </th>
                                        <td>
                                            @if (Model.DBCommand == "Add")
                                            {
                                                @Html.TextBoxFor(m => m.BorrowerModel.Borrower.BorrowerName, new { @class = "single-line scroll-field1", @onchange = "CheckIfNameExists(this);" }) 
                                            }
                                            else if (Model.DBCommand == "Edit")
                                            {
                                                if (Model.BorrwerNameIsEditable)
                                                {
                                                    @Html.TextBoxFor(m => m.BorrowerModel.Borrower.BorrowerName, new { @class = "single-line scroll-field1", @onchange = "CheckIfNameExists(this);" })      
                                                }
                                                else
                                                {
                                                    @Html.DisplayFor(m => m.BorrowerModel.Borrower.BorrowerName)
                                                    @Html.HiddenFor(m => m.BorrowerModel.Borrower.BorrowerName)
                                                }
                                            }
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>@Html.LabelFor(m => m.BorrowerModel.Borrower.BranchCode)</th>
                                        <td>
                                            @Html.HiddenFor(m => m.BorrowerModel.OldAgentID)
                                            @if (Model.DBCommand == "Add")
                                            {
                                                @Html.DropDownListFor(m => m.BorrowerModel.Borrower.BranchCode, Model.Branches, new { @id = "ddlBranches", @class = "single-line scroll-field1", @onchange = "GetAgents()" })
                                            }
                                            else
                                            {
                                                @Html.HiddenFor(m => m.BorrowerModel.Borrower.BranchCode)
                                                @Html.DisplayFor(m => m.BorrowerModel.Borrower.BranchCode)
                                            }
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>@Html.LabelFor(m => m.BorrowerModel.Borrower.AgentID)</th>
                                        <td>@Html.DropDownListFor(m => m.BorrowerModel.Borrower.AgentID, Model.Agents, new { @id = "ddlAgents", @class = "single-line scroll-field1" })</td>
                                    </tr>
                                    <tr>
                                        <th>@Html.LabelFor(m => m.BorrowerModel.Borrower.CreditLimit)</th>
                                        <td>
                                            @if (Model.DBCommand == "Add")
                                            {
                                                @Html.TextBoxFor(m => m.BorrowerModel.Borrower.CreditLimit, "{0:#,###,##0.00}", new { @class = "single-line scroll-field1", @onchange = "FormatMe(this);" })
                                            }
                                            else
                                            {
                                                if (Model.IsAdmin)
                                                {
                                                    @Html.TextBoxFor(m => m.BorrowerModel.Borrower.CreditLimit, "{0:#,###,##0.00}", new { @class = "single-line scroll-field1", @onchange = "FormatMe(this);" })
                                                }
                                                else
                                                {
                                                    @Html.HiddenFor(m => m.BorrowerModel.Borrower.CreditLimit)
                                                    @Html.DisplayFor(m => m.BorrowerModel.Borrower.CreditLimit)
                                                }
                                            }
                                        </td>                                       
                                    </tr>
                                    <tr>
                                        <th>@Html.LabelFor(m => m.BorrowerModel.Borrower.Address)</th>
                                        <td>
                                            @if (Model.DBCommand == "Add")
                                            {
                                                @Html.TextBoxFor(m => m.BorrowerModel.Borrower.Address, new {  @class = "single-line scroll-field1"})
                                            }
                                            else
                                            {
                                                if (Model.IsAdmin)
                                                {
                                                    @Html.TextBoxFor(m => m.BorrowerModel.Borrower.Address, new { @class = "single-line scroll-field1" })
                                                }
                                                else
                                                {
                                                    @Html.HiddenFor(m => m.BorrowerModel.Borrower.Address)
                                                    @Html.DisplayFor(m => m.BorrowerModel.Borrower.Address)
                                                }
                                            }
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>@Html.LabelFor(m => m.BorrowerModel.Borrower.ContactNumbers)</th>
                                        <td>
                                            <form class="form-horizontal m-t-md" action="#">
                                                <table style="width:100%">
                                                    <tr>
                                                        <td>
                                                            <div class="form-group">
                                                                <input id="txtContactNumber" type="text" class="form-control scroll-field1" data-mask="9999999999" placeholder="Cellphone Number"/>
                                                            </div>
                                                        </td>
                                                        <td style="text-align:center"><button type="button" class="btn btn-white" onclick="AddContactNumber()">Add</button></td>
                                                    </tr>
                                                    <tr>
                                                        <td><select id="selContactNumbers" multiple="multiple" class="single-line scroll-field1"></select></td>
                                                        <td style="text-align:center"><button type="button" class="btn btn-white" onclick="RemoveContactNumber()">Remove</button></td>
                                                    </tr>
                                                </table>
                                            </form>
                                            @Html.HiddenFor(m => m.BorrowerModel.Borrower.ContactNumbers, new { @id = "hdnContactNumbers" })
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>@Html.LabelFor(m => m.BorrowerModel.Borrower.EmailAddresses)</th>
                                        <td>@Html.TextBoxFor(m => m.BorrowerModel.Borrower.EmailAddresses, new { @class = "single-line scroll-field1" })</td>
                                    </tr>
                                    <tr>
                                        <th>@Html.LabelFor(m => m.BorrowerModel.Borrower.IsActive)</th>
                                        <td>@Html.CheckBoxFor(m => m.BorrowerModel.Borrower.IsActive, new { @checked = "checked" })</td>
                                    </tr>
                                </table>
                            </div>
                            <p></p>
                            <div class="col-lg-12">
                                <button type="submit" id="btnSave" class="btn btn-primary" name="command" value=@Model.DBCommand>Save</button>
                                <button type="submit" id="btnCancel" class="btn btn-primary cancel" name="command" value="Cancel">Cancel</button>
                            </div>
                        </fieldset>
                </div>
            }
            </div>

        </div>
    </div>
</div>

@section Styles{
@Styles.Render("~/plugins/jasnyBootstrapStyles")
@Styles.Render("~/plugins/dataPickerStyles")
}

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/plugins/dataPicker")
@Scripts.Render("~/plugins/jasnyBootstrap")

    <script type="text/javascript">
        $(document).ready(function () {

            $(window).keydown(function (event) {
                if (event.keyCode == 13) {
                    event.preventDefault();
                    return false;
                }
            });

            PopulateContactNumbers();
            
        });

        function PopulateContactNumbers()
        {
            var val = $('#hdnContactNumbers').val();
            var arr = val.split(" ");

            $.each(arr, function (i, txt) {
                if ($.trim(txt) != '') {
                    $('#selContactNumbers').append('<option>' + txt + '</option>');
                }
            });
        }

        function AddContactNumber()
        {
            var val = $('#txtContactNumber').val().trim();
            if (val != '') {

                var exists = false;
                
                $('#selContactNumbers > option').each(function (i) {
                    
                    if ($(this).text() == val) {
                        exists = true;
                    }
                });

                if (!exists) {
                    $('#selContactNumbers').append('<option>' + val + '</option>');
                    var newVal = $('#hdnContactNumbers').val() + ' ' + val;
                    $('#hdnContactNumbers').val(newVal.trim());
                    $('#txtContactNumber').val('');
                }
            }
            
        }

        function RemoveContactNumber()
        {
            $('#selContactNumbers > option').each(function (i) {
                var isSelected = $(this).is(':selected');
                if (isSelected) {
                    var val = $(this).val();
                    $(this).remove();
                    var newVal = $('#hdnContactNumbers').val().replace(val, '').replace('  ', ' ');
                    $('#hdnContactNumbers').val(newVal.trim());
                }
            });
        }

        function GetAgents()
        {
            var val = $('#ddlBranches').val();
            var subItems = "";

            $.getJSON("@Url.Action("GetAgents","Borrower")", { branchCode: val },
                function (data) {

                    $.each(data, function (index, item) {

                        subItems += "<option value='" + $.trim(item.Value) + "'>" + $.trim(item.Text) + "</option>"

                    });

                    $("#ddlAgents").html(subItems)
                });
        }

        function FormatMe(me) {
            var val = $(me).val().replace(',', '').replace(',', '').replace(',', '');

            if (isNaN(parseFloat(val)))
                val = 0.00;

            var decimalFormat = parseFloat(val).toFixed(2);
            var commaFormat = FormatNumber(decimalFormat);

            $(me).val(commaFormat);
        }

        function FormatNumber(nStr) {
            nStr += '';
            var x = nStr.split('.');
            var x1 = x[0];
            var x2 = x.length > 1 ? '.' + x[1] : '';
            var rgx = /(\d+)(\d{3})/;
            while (rgx.test(x1)) {
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
            }
            return x1 + x2;
        }

        function CheckIfNameExists(me)
        {
            var borrowerName = $(me).val();

            $.getJSON("@Url.Action("CheckIfNameExists", "Borrower")", { borrowerName: borrowerName },
                function (bln) {

                    if (bln) {
                        $('#valBorrowerNameExists').show();
                        $('#btnSave').prop('disabled', true);
                    }
                    else {
                        $('#valBorrowerNameExists').hide();
                        $('#btnSave').prop('disabled', false);
                    }
                });


        }

    </script>
}
